<?php
namespace app\index\controller;

use think\Controller;
use think\Session;
use think\Validate;

class Login extends Controller
{
    public function login()
    {
        $data = request()->post();

        $rule = [
                'u_name'  => 'require|/^[\x{4e00}-\x{9fa5}]{2,}$/u',
                'u_pwd'   => 'require| /^[\S]{6,}$/'
            ];
            
            $msg = [
                'u_name.require' => '名称必填项',
                'u_name./^[\x{4e00}-\x{9fa5}]{2,}$/u'     => '用户名不能为空、且必须是汉字',
                'u_pwd.require' => '名称必填项',
                'u_pwd. /^[\S]{6,}$/ '     => '不能为空、且长度必须大于6位'
            ];
            
            $validate = new Validate($rule, $msg);
            $result   = $validate->check($data);
            if( !$result )
            {
                return json(array('code'=>1,'msg'=>$validate->getError(),'data'=>''));
            }

            $where = [];
            $where['u_name'] = $data['u_name'];
            if( $where['u_name'] != $data['u_name'])
            {
                return json(array('code'=>1,'msg'=>'用户名错误!!!!','data'=>''));
            }
           $res = model("Users") -> getOne($where);

           if(!$res)
           {
            return json(array('code'=>1,'msg'=>'数据错误!!!!','data'=>''));
           }

           if($res['u_pwd'] != $data['u_pwd'])
           {
            return json(array('code'=>1,'msg'=>"密码错误!!!",'data'=>''));
           }

           Session::set('name',$res);
        
           return json(array('code'=>0,'msg'=>"登录成功!!!",'data'=>''));

    }
}
?>